const pool = require("./db.js");

class GroupsPeopleDao {
    async addGroupsPeople(groupId, type, userId) {
        console.log(groupId, type, userId);
        const [rows, fields] = await pool.execute(
            "INSERT INTO Groups_People (group_id, type, user_id) VALUES (?, ?, ?)",
            [groupId, type, userId]
        );
        return rows;
    }

    async deleteGroupsPeopleById(group_people_id) {
        const [rows, fields] = await pool.execute(
            "DELETE FROM Groups_People WHERE group_people_id = ?",
            [group_people_id]
        );
        return rows;
    }

    async deleteGroupsPeopleByGroupID(groupId) {
        const [rows, fields] = await pool.execute(
            "DELETE FROM Groups_People WHERE group_id = ?",
            [groupId]
        );
        return rows;
    }

    async selectGroupsPeopleByTypeAndGroupID(groupID){
        const [rows, fields] = await pool.execute(
            "SELECT DISTINCT * FROM Groups_People WHERE group_id = ? AND type in (2,3)",
            [groupID]
        );
        return rows;
    }

    async selectGroupsPeopleByGroupPeopleId(groupPeopleID) {
        const [rows, fields] = await pool.execute(
            "SELECT DISTINCT * FROM Groups_People WHERE group_people_id = ?",
            [groupPeopleID]
        );
        return rows;
    }

    async selectGroupsPeopleByGroupIDAndUserID(GroupID,UserID){
        const [rows, fields] = await pool.execute(
            "SELECT DISTINCT * FROM Groups_People WHERE group_id = ? AND user_id = ?",
            [GroupID,UserID]
        );
        return rows;
    }

    async selectGroupsPeopleByGroupID(groupId) {
        const [rows, fields] = await pool.execute(
            "SELECT DISTINCT * FROM Groups_People WHERE group_id = ?",
            [groupId]
        );
        return rows;
    }

    async selectGroupsPeopleByUserID(userId) {
        const [rows, fields] = await pool.execute(
            "SELECT DISTINCT * FROM Groups_People WHERE user_id = ?",
            [userId]
        );
        return rows;
    }

    async updataType(group_people_id, type) {
        const [rows, fields] = await pool.execute(
            "UPDATE Groups_People SET type = ? WHERE group_people_id = ?",
            [type, group_people_id]
        );
        return rows;
    }
}

module.exports = new GroupsPeopleDao();